package com.xiao.gulimall.ware.service.impl;

import com.xiao.gulimall.ware.entity.PurchaseDetailEntity;
import com.xiao.gulimall.ware.service.PurchaseDetailService;
import com.xiao.gulimall.ware.vo.MergoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiao.common.utils.PageUtils;
import com.xiao.common.utils.Query;

import com.xiao.gulimall.ware.dao.PurchaseDao;
import com.xiao.gulimall.ware.entity.PurchaseEntity;
import com.xiao.gulimall.ware.service.PurchaseService;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {


    @Autowired
    private PurchaseDetailService purchaseDetailService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void purchaseMerge(MergoVo mergoVo) {
        /*
         * 1.mergoVo 中有一个参数为采购单id
         * 这个采购单id可能为空
         * 1.1 如果采购单id为空
         * 需要新增一条记录
         * 1.2 如果不为空
         * 修改采购单的中的记录
         * */
        //1.1如果采购单为空
        // item'中防止的所有采购需要id
        List<Long> items = mergoVo.getItems();
        //通过id 查询到所有的查询需求信息
        List<PurchaseDetailEntity> detailEntityList = (List<PurchaseDetailEntity>) purchaseDetailService.listByIds(items);
        
        PurchaseEntity purchaseEntity = null;
        
        if(mergoVo.getPurschaseId() != null){
            //如果不为空  需要修改总价
            Long purschaseId = mergoVo.getPurschaseId();
             purchaseEntity = baseMapper.selectById(purschaseId);
             purchaseEntity.setUpdateTime(new Date());
             baseMapper.updateById(purchaseEntity);
             
        }else {
            // 1.1 如果采购单 id为空
            purchaseEntity= new PurchaseEntity();
            purchaseEntity.setWareId(detailEntityList.get(0).getWareId());
            purchaseEntity.setStatus(0); //新建的状态
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            baseMapper.insert(purchaseEntity);
        }
        final Long purchaseId = purchaseEntity.getId();
        List<PurchaseDetailEntity> collect = detailEntityList.stream().map(item -> {
            //1求和
            //TODO 需要feign调用商品服务查询每个查询需求中的商品信息 得到单价
            item.setPurchaseId(purchaseId);
            item.setStatus(1);
            return item;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(collect);
    }













}